package com.hmall.order.mq;

import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Slf4j
@Component
/**
 * mq消费端
 */
public class RabbitListenerConsumer {

    @Autowired
    private OrderMapper orderMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cxk.queue", durable = "true"),
            exchange = @Exchange(name = "cxk.direct",delayed = "true"),
            key = "ttl"
    ))
    public void dazyQueue(String id){
        log.info("蔡徐坤大小姐接收到5分钟后的延迟消息：{}", id);
        Order order = orderMapper.selectById(id);
        //如果已支付则更新订单状态
        if (order.getStatus() == 1){
            order.setUpdateTime(new Date());
            order.setCloseTime(new Date());
        }else{
            order.setStatus(5);
            order.setCloseTime(new Date());
        }
        //更新
        this.orderMapper.updateById(order);
    }

}